//
//  topView.m
//  首页
//
//  Created by 赵东亮 on 2018/7/3.
//  Copyright © 2018年 赵东亮. All rights reserved.
//

#import "topView.h"
#import "TbCategoryTitleModel.h"


@interface topView ()

@property (strong, nonatomic) UIScrollView *titleScrollView;

@property (nonatomic, strong) NSMutableArray *labelArray;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSArray *modelArray;
@property (strong, nonatomic) UIImageView *bgIv;
@property (strong, nonatomic) UIImageView *selectedLogoIV;
@property (assign, nonatomic) BOOL isSelectedPicture;
@property (assign, nonatomic) NSInteger controllerType;
@property (strong, nonatomic) NSMutableArray *btnViewArr;
@property (strong, nonatomic) NSMutableArray *textArr;
@property (nonatomic, strong) NSMutableArray *btnTapArray;
@property (strong, nonatomic) NSArray *categoryArr;

@property (nonatomic, assign) NSInteger temIndex;

@end

@implementation topView


- (NSMutableArray *)btnTapArray {
    if (!_btnTapArray) {
        _btnTapArray = [[NSMutableArray alloc] init];
    }
    return _btnTapArray;
}

- (instancetype)initWithControllerType:(NSInteger)controllerType {
    self = [super init];
    
    if (self) {
        self.temIndex = 0;
        self.backgroundColor = [UIColor clearColor];
        self.controllerType = controllerType;
        self.isSelectedPicture = NO;
        [self setupTopUI];
    }
    
    return self;
}

#pragma mark -btnView点击事件
- (void)btnViewGestureClick:(UIGestureRecognizer *)recognize {
    NSLog(@"自营、淘宝等按钮点击");
    UIView *bg = recognize.view;
    if (self.temIndex == bg.tag && bg.tag != 1) {
        return;
    }
    self.temIndex = bg.tag;
    if (bg.tag == 0) {
        // 自营
        BaseTabbarViewController *Vc = [BaseTabbarViewController sharedMainTabBarVC];
        Vc.selectedIndex = 1;
        UIWindow *keywindow = [[UIApplication sharedApplication].windows firstObject];
        keywindow.rootViewController = Vc;
        [keywindow makeKeyAndVisible];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NSNotificationTopClick" object:@{@"tag" : [NSString stringWithFormat:@"%zd", bg.tag]}];
    }else  {

        UIImageView *logoIV = nil;
        UILabel *nameL = nil;
        
        for (int i = 0; i < self.btnViewArr.count; i++) {
            
            UIView *subV = self.btnViewArr[i];
            UILabel *textL = self.textArr[i];
            
            if (subV.tag == bg.tag) {
                textL.textColor = Colorff0000;
            }else {
                textL.textColor = Color333333;
            }
            
            if (subV.tag == 0) {
                textL.textColor = Color333333;
            }
            
        }
        
        for (UIView *view in bg.subviews) {
            
            if ([view isKindOfClass:[UIImageView class]] || [view isMemberOfClass:[UIImageView class]] ) {
                logoIV = (UIImageView *)view;
            }
            
            if ([view isKindOfClass:[UILabel class]] || [view isMemberOfClass:[UILabel class]]) {
                nameL = (UILabel *)view;
            }
        }

        if (self.topButtonClick) {
            
            TopNavgatioModel *model = self.modelArray[bg.tag];
            
            self.topButtonClick(model.nid);
        }
    }
    
    if (bg.tag == 0) {
        
    }else if (bg.tag == 1) {
        
//        self.bgIv.image = [UIImage imageNamed:@"HotSelfSupportBg2"];

    }else if (bg.tag == 2) {
        
//        self.bgIv.image = [UIImage imageNamed:@"HotSelfSupportBg3"];

    }else if (bg.tag == 3) {
        
//        self.bgIv.image = [UIImage imageNamed:@"HotSelfSupportBg4"];
  
    }
    
    
}

- (void)setupTopUI {
    
    [self addSubview:self.titleScrollView];
    __weak typeof(self) weakSelf = self;
    
    [self.titleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.top.mas_equalTo(weakSelf.mas_top);
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
    }];
    
    [self createTitleScrollView];
    
}

#pragma mark -标题Scrollview
- (void)createTitleScrollView {

    UIImageView *bgIv = [[UIImageView alloc] init];
    self.bgIv = bgIv;
    
    if (self.type == 1) {
        bgIv.image = [UIImage imageNamed:@"topbj"];
    }else {
//        bgIv.image = [UIImage imageNamed:@"HotSelfSupportBg2"];
    }
    
    [self.titleScrollView addSubview:bgIv];
    __weak typeof(self) weakSelf = self;
    
    CGFloat height = (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? jisuan(300) : jisuan(260);
    
    [bgIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mas_top);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.height.mas_equalTo(height);
    }];
    
    [self createBtnViewWith:self.titleScrollView];
    
}

- (void)createBtnViewWith:(UIView *)parentView {
    
    CGFloat width = WIDTH / 4.0;
    CGFloat height = (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? jisuan(250) : jisuan(220);
    
    for (int i = 0 ; i < 4; i ++) {
        
        UIView *btnView = [[UIView alloc] init];
        btnView.tag = i;
        btnView.frame = CGRectMake(i * width, 0, width, height);
        UITapGestureRecognizer *btnViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnViewGestureClick:)];
        [btnView addGestureRecognizer:btnViewGesture];
        [self.btnTapArray addObject:btnViewGesture];
        [parentView addSubview:btnView];
        [self.btnViewArr addObject:btnView];
        
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = Font(24);
        label.textColor = Color333333;
        label.tag = i;
        [self.textArr addObject:label];
        
        [btnView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(btnView.mas_centerX);
            make.bottom.mas_equalTo(btnView.mas_bottom).with.offset(-jisuan(30));
        }];
        
//        UIImageView *logoIV = [[UIImageView alloc] init];       
//        if (i == 1) {
//            label.textColor = Colorff0000;
//            self.selectedLogoIV = logoIV;
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"NSNotificationTopClick" object:@{@"tag" : [NSString stringWithFormat:@"%d", i]}];
//        }
//
//        [btnView addSubview:logoIV];
        
//        [logoIV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.mas_equalTo(btnView.mas_centerX);
//            make.bottom.mas_equalTo(label.mas_top).with.offset(-jisuan(3));
//            make.width.mas_equalTo(jisuan(108));
//            make.height.mas_equalTo(jisuan(108));
//        }];
        
        [self.labelArray addObject:label];
//        [self.imageArray addObject:logoIV];
        
    }
    
    
}


- (void)reloadButtonImage:(NSArray *)array selectIndex:(NSInteger)selectIndex {
   
    self.modelArray = array;
    for (int i = 0 ; i < self.imageArray.count; i ++) {
        UIImageView *image = self.imageArray[i];
        UILabel *label = self.labelArray[i];
        TopNavgatioModel *model = array[i];
        label.text = model.title;
        [image sd_setImageWithURL:[NSURL URLWithString:model.photo]];
    }
    
    if (array.count>0) {
        [self btnViewGestureClick:self.btnTapArray[selectIndex]];
    }
}

#pragma mark -lazy
- (UIScrollView *)titleScrollView {
    
    if (!_titleScrollView) {
        _titleScrollView = [[UIScrollView alloc] init];
        _titleScrollView.showsVerticalScrollIndicator = NO;
        _titleScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _titleScrollView;
}

- (NSMutableArray *)textArr {
    
    if (!_textArr) {
        
        _textArr = [NSMutableArray array];
        
    }
    return _textArr;
}

- (NSMutableArray *)btnViewArr {
    
    if (!_btnViewArr) {
        
        _btnViewArr = [NSMutableArray array];
        
    }
    return _btnViewArr;
}

- (NSMutableArray *)labelArray {
    if (!_labelArray) {
        _labelArray = [[NSMutableArray alloc] init];
    }
    return _labelArray;
}

- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        _imageArray = [[NSMutableArray alloc] init];
    }
    return _imageArray;
}

@end
